<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/25
 * Time: 16:13
 */

namespace App\Model\service\admin;


use App\Common;
use App\Model\Goods;
use Illuminate\Support\Facades\Validator;

class GoodsLogic
{

    public function listGoods($input_data){
        $validator = Validator::make($input_data, [
//            'title' => 'required|unique:posts|max:255',
            'order' => 'in:0,1',
            'page' => 'required',
            'page_size' => 'required',
        ]);

        if ($validator->fails()) {
            return Common::getInstance()->resp('422',$validator->errors());
        }
        $page = $input_data['page'];
        $page_size = $input_data['page_size'];
        //TODO::待做搜索&排序条件
        $orderList = array('goods_id asc','goods_id desc');
        $orderBy = isset($input_data['order'])?$orderList[$input_data['order']]:$orderList[0];
        $cond = '1=1';
        $db = new Goods();
        $rst = $db->listByPage($cond,$page,$page_size,$orderBy);
        if($rst){
            $total = $db->countGoods($cond);
            $result['data']  = $rst;
            $result['total_page'] = Common::getInstance()->getPageCount($page_size,$total);
            $result['page_size'] = $page_size;
            $result['current'] = $page;
            return Common::getInstance()->resp('200','',$result);
        }else{
            return Common::getInstance()->resp('422','没有数据');
        }



    }
}